function C=refind_saccade(C,plot_which)
% C=refind_saccade(C,plot_which)
% a function to find target saccade onset


if nargin==0
    help refind_saccade
    return
end

    
speed=C.speed(C.e_start(plot_which):C.e_end(plot_which));
time= C.time (C.e_start(plot_which):C.e_end(plot_which))-C.cue_tgt_on(plot_which);
eye_y=C.eye_y(C.e_start(plot_which):C.e_end(plot_which));
eye_x=C.eye_x(C.e_start(plot_which):C.e_end(plot_which));

            fix_noise=speed(speed<70); % most people use a fixed 50dps for saccade onset. here i remove all speeds over 70dps ... 
            sac_thres=max([20 mean(fix_noise)+(3*std(fix_noise))]); % and find the mean and 3std to create a variable threshold to compensate for different noise levels
            sac_pts=find(speed>(sac_thres)); % sometimes 50's too low and sometimes it's too high.
            sac_start_pos=(sac_pts(find(diff(sac_pts)>3)+1));
            sac_start_pos=[sac_pts(1); sac_start_pos];
            sac_end_pos=(sac_pts(find(diff(sac_pts)>3)));
            sac_end_pos=[sac_end_pos;sac_pts(end)];
            sac_start_times=time(sac_start_pos);
            sac_end_times=time(sac_end_pos);
            sac_dur=sac_end_times-sac_start_times; % make sure the 'saccades' last... 
            sac_start_pos(sac_dur<=20)=[];% ignore if too short: <=20 ms long
            sac_end_pos  (sac_dur<=20)=[];% ignore if too short: <=20 ms long
            clear sac_dur;
            sac_pts2=[];
            fast_enought=[];
            for i=1:length(sac_start_pos)
                sac_pts2=[sac_pts2 sac_start_pos(i):sac_end_pos(i)];
                fast_enought(i)=sum(speed(sac_start_pos(i):sac_end_pos(i))>70);
            end
            sac_start_pos(fast_enought==0)=[];% ignore if too slow: peak speed <=70 dps 
            sac_end_pos  (fast_enought==0)=[];% ignore if too slow: peak speed <=70 dps 
            
            sac_start_times=time(sac_start_pos);
            sac_end_times=time(sac_end_pos);

uimenufcn(gcf,'WindowCommandWindow');
clc;
fig(200);clf; 
set(gcf,'Position',[2 342 1275 592])
subplot(2,1,1);hold on; tickout


plot(time,speed,'.-g')
plot(time(sac_pts2),speed(sac_pts2),'.','color',[.8 .8 .8]);
h=vline(sac_start_times,'k');
hline(sac_thres);
%%%%%%%%%%%%%%%%%%%%%%%
%find tgt sac
bad_sac=0;
bad_sac(length(sac_start_times),1)=0;%create a fuzzy boolean
bad_sac(sac_start_times<100)=1; % ditch if too early
bad_sac(sac_start_times>1000)=1; % ditch if too late
%bad_sac(abs_speed(find_any(temp_eye_time,(round((sac_end_times-sac_start_times)/2)+sac_start_times)))<120)=1; % ditch if too slow <120 dps
if length(sac_start_pos)>1 % if multiple saccades
    bad_sac([abs(eye_x(sac_start_pos))>5 & abs(eye_y(sac_start_pos))>5 ])=1; % if the saccade starts with the eye 6 deg (x&y) away from the fix )
end
vline(sac_start_times(bad_sac==1));

tsac_on_time =min(sac_start_times(find(~bad_sac))); %find first saccade that matches guidlines
tsac_off_time=min(sac_end_times  (find(~bad_sac))); %find first saccade that matches guidlines
tsac_on_pos  =min(sac_start_pos  (find(~bad_sac))); %find first saccade that matches guidlines
tsac_off_pos =min(sac_end_pos    (find(~bad_sac))); %find first saccade that matches guidlines
tsac_range=tsac_on_pos:tsac_off_pos;

fig(200);set(gcf,'Position',[2 342 1275 592])
subplot(2,1,2);hold on; tickout
plot(time,eye_x,'.-b');
plot(time,eye_y,'.-r');
vline(tsac_on_time,'k');
drawnow


if tsac_on_time
    C.srt(plot_which)=tsac_on_time;
    C.sac_on(plot_which)=tsac_on_time+C.cue_tgt_on(plot_which);
    C.sac_off(plot_which)=tsac_off_time+C.cue_tgt_on(plot_which);
end

return;